/**
 * 基础菜单 商品管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>仓库管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="condition" class="user-search">
      <el-form-item label="搜索：">
        <el-input
          size="small"
          v-model="condition.name"
          placeholder="输入仓库名称"
        ></el-input>
      </el-form-item>

      <el-form-item label="">
        <el-select
          size="small"
          v-model="condition.aid"
          placeholder="请选择管理人"
        >
          <el-option label="仓库人员" value=""></el-option>
          <el-option
            v-for="(item, index) in admins"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="">
        <el-select
          size="small"
          v-model="condition.status"
          placeholder="请选择仓库状态"
        >
          <el-option label="状态" value=""></el-option>
          <el-option label="正常" value="01"></el-option>
          <el-option label="已冻结" value="00"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEdit('add')"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->

    <div>
      <el-button
        size="mini"
        type="danger"
        @click="dels"
        style="margin-bottom: 5px"
        >批量删除</el-button
      >
    </div>

    <el-table
      size="small"
      :data="listData"
      @selection-change="selectChange"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>

      <!-- 仓库名称 -->
      <el-table-column sortable prop="name" label="仓库名称" min-width="150">
      </el-table-column>

      <!-- 仓库管理人员名字 -->
      <el-table-column
        sortable
        prop="aid"
        label="管理人员"
        min-width="150"
        :formatter="nameFormat"
      >
      </el-table-column>

      <!-- 仓库管理人员联系方式 -->
      <el-table-column
        sortable
        prop="aid"
        label="联系方式"
        min-width="150"
        :formatter="phoneFormat"
      >
      </el-table-column>

      <!-- 仓库地址 -->
      <el-table-column sortable prop="address" label="仓库地址" min-width="150">
      </el-table-column>

      <!-- 创建时间 -->
      <el-table-column
        sortable
        prop="createTime"
        label="创建时间"
        min-width="150"
      >
      </el-table-column>

      <!-- 修改时间 -->
      <el-table-column
        sortable
        prop="updateTime"
        label="修改时间"
        min-width="150"
      >
      </el-table-column>

      <!-- 状态 -->
      <el-table-column prop="status" label="状态" min-width="80">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="01"
            inactive-value="00"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="setStatus(scope.row.id, scope.row.status)"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            @click="handleEdit('view', scope.row)"
            >详情</el-button
          >

          <el-button size="mini" @click="handleEdit('edit', scope.row)"
            >编辑</el-button
          >

          <el-button
            size="mini"
            type="primary"
            @click="openView(scope.row.materials)"
            >仓库详情</el-button
          >

          <el-button size="mini" type="danger" @click="del(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="30%"
      @close="closeDialog"
    >
      <el-form
        label-width="100px"
        :model="editForm"
        :rules="rules"
        ref="editForm"
      >
        <el-form-item label="仓库名称" prop="name">
          <el-input
            size="small"
            v-model="editForm.name"
            auto-complete="off"
            placeholder="请输入仓库名称"
            :disabled="isView"
          ></el-input>
        </el-form-item>

        <el-form-item label="管理人员" prop="aid">
          <el-select
            size="small"
            v-model="editForm.aid"
            placeholder="请选择管理人员"
            :disabled="isView"
          >
            <el-option
              v-for="(item, index) in admins"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="仓库地址" prop="address">
          <el-input
            size="small"
            v-model="editForm.address"
            :disabled="isView"
            placeholder="请输入仓库地址"
          ></el-input>
        </el-form-item>

        <el-form-item label="描述" prop="remarks">
          <el-input
            type="textarea"
            v-model="editForm.remarks"
            placeholder="请输入该仓库的详情描述"
            :rows="3"
            :disabled="isView"
          ></el-input>
        </el-form-item>

        <el-form-item label="材料" v-if="isView">
          <el-table
            :data="editForm.materials"
            border
            stripe
            style="width: 100%"
          >
            <el-table-column type="index" width="60"> </el-table-column>

            <el-table-column prop="iconUrl" label="样图" min-width="120">
              <template slot-scope="scope">
                <el-image
                  style="width: 100px; height: 100px"
                  :src="scope.row.iconUrl"
                  fit="fit"
                ></el-image>
              </template>
            </el-table-column>

            <el-table-column prop="name" label="材料名称" width="180">
            </el-table-column>

            <el-table-column prop="type" label="材料类型" width="180">
            </el-table-column>

            <el-table-column prop="purchasePrice" label="进价">
            </el-table-column>

            <el-table-column prop="purchasePrice" label="售价">
            </el-table-column>

            <el-table-column prop="count" label="数量"> </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('editForm')"
          >保存</el-button
        >
      </div>
    </el-dialog>
    <!--仓库详情界面-->
    <el-dialog
      :title="title"
      :visible.sync="materialVisible"
      width="80%"
      @close="closeMaterial"
    >
      <el-table :data="materials" border stripe style="width: 100%">
        <el-table-column type="index" width="60"> </el-table-column>

        <el-table-column prop="iconUrl" label="样图" min-width="120">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.iconUrl"
              fit="fit"
            ></el-image>
          </template>
        </el-table-column>

        <el-table-column prop="name" label="材料名称" min-width="180">
        </el-table-column>

        <el-table-column
          prop="type"
          label="材料类型"
          min-width="180"
          :formatter="typeFormat"
        >
        </el-table-column>

        <el-table-column prop="purchasePrice" label="进价" min-width="120">
        </el-table-column>

        <el-table-column prop="purchasePrice" label="售价" min-width="120">
        </el-table-column>

        <el-table-column prop="count" label="数量" min-width="120">
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "../../components/Pagination";
import dataOrigin from "../../assets/data.json";
export default {
  inject: ["reload"],
  data() {
    //校验名称唯一
    var validateName = async (rule, value, callback) => {
      let c = {
        id: this.editForm.id,
        name: value,
      };
      var d = await this.$axios.post("/warehouse/checkName", c);
      if (d.code == 200) {
        if (d.message == "0") {
          callback();
        } else {
          callback("仓库名称已存在");
        }
      }
    };
    return {
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      materialVisible: false, //控制仓库详情显示与隐藏
      isView: false, //是否为详情页面
      isAdd: false, //是否为添加页面
      title: "添加",
      types: dataOrigin.materialTypes,
      admins: [],
      editForm: {
        id: "",
        name: "",
        aid: "",
        address: "",
        remarks: "",
        status: "",
        admin: {},
        materials: [],
      },
      materials: [],
      // rules表单验证
      rules: {
        name: [
          { required: true, message: "请输入仓库名称", trigger: "blur" },
          {
            validator: validateName,
            trigger: "blur",
          },
        ],
        aid: [{ required: true, message: "请选择管理人员", trigger: "blur" }],
        address: [
          { required: true, message: "请输入仓库地址", trigger: "blur" },
        ],
      },
      condition: {
        currentPage: 1,
        pageSize: 10,
        name: "",
        aid: "",
        status: "",
      },
      listData: [], //用户数据
      // 选择数据
      selectdata: [],
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  // 注册组件
  components: {
    Pagination,
  },

  /**
   * 创建完毕
   */
  created() {
    this.init();
    this.getActiveAdmin();
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    //初始化表格
    async init() {
      this.loading = true;
      var d = await this.$axios.post("/warehouse/page", this.condition);
      if (d.code == 200) {
        this.listData = d.data.records;
        this.pageparm.currentPage = d.data.current;
        this.pageparm.pageSize = d.data.size;
        this.pageparm.total = d.data.total;
        console.log(this.listData);
      } else {
        this.$message.error(d.message);
      }
      this.loading = false;
    },

    //获取所有正常状态下的仓库人员
    async getActiveAdmin() {
      var d = await this.$axios.get("/admin/activeAll");
      if (d.code == 200) {
        this.admins = d.data;
      }
    },

    //修改用户状态
    async setStatus(id, status) {
      this.loading = true;
      let param = {
        id: id,
        status: status,
      };
      var d = await this.$axios.put("/warehouse/status", param);
      if (d.code == 200) {
        this.$message.success(d.message);
      } else {
        this.$message.error(d.message);
      }
      this.loading = false;
    },

    //管理人员姓名格式化
    nameFormat(row, column, cellValue, index) {
      if (row.admin != null) {
        return row.admin.name;
      } else {
        return "暂无管理人员";
      }
    },

    //管理人员联系方式格式化
    phoneFormat(row, column, cellValue, index) {
      if (row.admin != null) {
        return row.admin.phone;
      } else {
        return "暂无管理人员";
      }
    },

    //类型格式化
    typeFormat(row, column, cellValue, index) {
      for (let i = 0; i < this.types.length; i++) {
        if (cellValue == this.types[i].value) {
          return this.types[i].name;
        }
      }
      return "未知错误";
    },

    // 分页插件事件
    callFather(parm) {
      this.condition.currentPage = parm.currentPage;
      this.condition.pageSize = parm.pageSize;
      this.init();
    },
    // 搜索事件
    search() {
      this.init();
    },

    //显示编辑界面
    handleEdit: function (name, row) {
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        this.title = "修改";
        this.isAdd = false;
        this.isView = name == "view";
        this.editForm.id = row.id;
        this.editForm.name = row.name;
        this.editForm.aid = row.aid;
        this.editForm.address = row.address;
        this.editForm.remarks = row.remarks;
        this.editForm.admin = row.admin;
        this.editForm.materials = row.materials;
      } else {
        this.title = "添加";
        this.isAdd = true;
        this.isView = false;
      }
    },

    //显示仓库详情
    openView(materials) {
      this.materialVisible = true;
      this.materials = materials;
    },

    //关闭仓库详情
    closeMaterial() {
      this.materialVisible = false;
      this.materials = [];
    },

    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(async (valid) => {
        if (valid) {
          if (this.isAdd) {
            //添加
            var d = await this.$axios.post("/warehouse/add", this.editForm);
            if (d.code == 200) {
              this.$message.success(d.message);
              this.reload();
            } else {
              this.$message.error(d.message);
            }
          } else {
            //编辑
            var d = await this.$axios.put("/warehouse/update", this.editForm);
            if (d.code == 200) {
              this.$message.success(d.message);
              this.reload();
            } else {
              this.$message.error(d.message);
            }
          }
        } else {
          return false;
        }
      });
    },

    // 删除
    del(id) {
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true;
          var d = await this.$axios.delete("/warehouse/del/" + id);
          if (d.data.code == 200) {
            this.$message.success(d.message);
            this.loading = false;
            this.reload();
          } else {
            this.$message.error(d.message);
            this.loading = false;
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 选择复选框事件
    selectChange(val) {
      this.selectdata = val;
    },

    // 批量删除
    dels() {
      if (this.selectdata.length <= 0) {
        return;
      }
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true;
          let ids = [];
          for (let i = 0; i < this.selectdata.length; i++) {
            ids.push(this.selectdata[i].id);
          }
          var d = await this.$axios.post("/warehouse/dels", ids);
          if (d.code == 200) {
            this.$message.success(d.message);
            this.loading = false;
            this.reload();
          } else {
            this.$message.error(d.message);
            this.loading = false;
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
      this.editForm = {
        id: "",
        name: "",
        aid: "",
        address: "",
        remarks: "",
        status: "",
        admin: {},
        materials: [],
      };
      this.$refs.editForm.resetFields();
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 